/*
  This file is a part of hashutils package.
  (c) 2004 Zeljko Vrba <zvrba@globalnet.hr>

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sfio.h>
#include <cdt.h>
#include <vmalloc.h>
#include <mycrypt.h>
#include "main.h"

static char rcsid[] = "$Id: hcksum.c 2 2007-03-29 14:43:55Z zeljko.vrba $";

const char *Main_argv0;

static void print_hash(Sfio_t *f, const char *h, int hlen)
{
	int i;

	for(i = 0; i < hlen; i++) sfprintf(f, "%02x", (unsigned char)h[i]);
}

static void cksum_lines(Sfio_t *f, Sfio_t *out, int halg)
{
	char *record;
	char hash[256];

	while((record = sfgetr(f, '\n', 0))) {
		unsigned long outlen = 256;
		int err;

		if((err = hash_memory(halg, (unsigned char*)record,
							  sfvalue(f), hash, &outlen)) != CRYPT_OK) {
			sfprintf(sfstderr, "FATAL: crypto error: %s, exiting.\n",
					 error_to_string(err));
			exit(1);
		}

		print_hash(out, hash, outlen);
		sfprintf(out, "\n");
	}
}

static void cksum_file(Sfio_t *f, Sfio_t *out, int halg)
{
	hash_state md;
	char hash[256];
	unsigned long outlen = hash_descriptor[halg].hashsize;
	Void_t *buf;
	
	if(outlen > 256) {
		sfprintf(sfstderr, "FATAL: internal error. buffer too small.");
		abort();
	}
	
	hash_descriptor[halg].init(&md);
	while((buf = sfreserve(f, -1, 0))) {
		int len = sfvalue(f);
		hash_descriptor[halg].process(&md, buf, len);
	}
	if(sferror(f)) {
		sfprintf(sfstderr, "ERROR reading file: sfgetr: %s\n", strerror(errno));
		exit(1);
	}
	hash_descriptor[halg].done(&md, hash);
	print_hash(out, hash, outlen);
	sfprintf(out, "\n");
}



/******************************************************************************
 * Implementation of 'main' interface.
 *****************************************************************************/

static int hash_target = 0;		/* 1=file, 2=line */

void Main_usage(const char *progname)
{
	sfprintf(sfstderr, "USAGE: %s OPTION HASH FILE\n", progname);
	sfprintf(sfstderr, "  -f             hash entire file\n");
	sfprintf(sfstderr, "  -l             hash each line\n");
	sfprintf(sfstderr, "\n");
	
	sfprintf(sfstderr, "HASH is one of the following (displayed as HASH/byte length):\n");
	Main_print_hashes();
	exit(1);	
}

int Main_process_option(int argc, char **argv, int *arg)
{
	if(!strcmp(argv[*arg], "-f")) {
		if(hash_target) Main_usage("ERROR: only ONE of -f and -l must be specified.");
		hash_target = 1;
		return 1;
	}

	if(!strcmp(argv[*arg], "-l")) {
		if(hash_target) Main_usage("ERROR: only ONE of -f and -l must be specified.");
		hash_target = 2;
		return 1;
	}

	return 0;
}

void Main_process_files(const char *fname1, const char *fname2, int halg)
{
	Sfio_t *file = Main_file_open(fname1);
	if(hash_target == 1) cksum_file(file, sfstdout, halg);
	else if(hash_target == 2) cksum_lines(file, sfstdout, halg);
	else {
		Main_usage("ERROR: one of -f and -l MUST be specified.");
	}
}

